package cn.wolfcode.wms.web.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.wms.domain.OrderBill;
import cn.wolfcode.wms.domain.OrderBillItem;
import cn.wolfcode.wms.mapper.OrderBillItemMapper;
import cn.wolfcode.wms.query.OrderBillQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IOrderBillService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.util.UserContext;

@Controller
@RequestMapping("/orderBill")
public class OrderBillController {
	@Autowired
	private IOrderBillService service;
	@Autowired
	private ISupplierService supplierService;
	
	@RequestMapping("/list")
	@RequiredPermission("订单列表")
	public String list(Model model,@ModelAttribute("qo")OrderBillQueryObject qo){
		model.addAttribute("suppliers", supplierService.selectAll());
		model.addAttribute("result", service.query(qo));
		return "orderBill/list";
	}
	@RequestMapping("/delete")
	@ResponseBody
	@RequiredPermission("订单删除")
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}
	
	@RequiredPermission("订单编辑")
	@RequestMapping("/input")
	public String input(Long id,Model model){
		model.addAttribute("suppliers", supplierService.selectAll());
		if (id!=null) {
			OrderBill orderBill = service.selectByPrimaryKey(id);
			model.addAttribute("orderBill", orderBill);
		}
		return "orderBill/input";
	}
	@RequiredPermission("订单保存或更新")
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(OrderBill orderBill){   
		JsonResult jsonResult = new JsonResult();
		try {
			if (orderBill.getId()!=null) {
				service.updateByPrimaryKey(orderBill);
			}else{
				service.insert(orderBill);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.markMsg("保存失败");
		}
		return jsonResult;
	}
	
	@RequiredPermission("查看订单")
	@RequestMapping("/show")
	public String show(Long id,Model model){
		if (id!=null) {
			OrderBill orderBill = service.selectByPrimaryKey(id);
			model.addAttribute("orderBill", orderBill);
		}
		return "orderBill/show";
	}

	
	@RequestMapping("/audit")
	@RequiredPermission("审核订单")
	@ResponseBody
	public JsonResult audit(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.auditByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("审核失败");
		}
		return result;
	}

	
}
